package com.tkjt.pms.modules.kpinew.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.tkjt.pms.common.entity.TkIndexValueAudit;
import com.tkjt.pms.modules.common.service.PMSCommonService;
import com.tkjt.pms.modules.common.service.TkIndexValueAuditService;
import com.tkjt.pms.modules.kpinew.service.KpiNewService;
import com.tkjt.pms.modules.kpinew.vo.KpiViewVo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/api/pms")
public class TkPmsKpisController {
    @Autowired
    private KpiNewService kpiNewService;
    @Autowired
    private PMSCommonService commonService;
    @Autowired
    private TkIndexValueAuditService tkIndexValueAuditService;
    /**
     * kpi看板数据获取
     * @param kpiViewVo
     * @return
     */
    @RequestMapping("kpiView")
    public Result kpiView(@RequestBody KpiViewVo kpiViewVo) {
        try {
            return kpiNewService.kpiView(kpiViewVo);
        }catch (Exception e){e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }

    /**
     * kpi看板查询条件
     * @return
     */
    @RequestMapping("kpiCondition")
    public Result kpiIndexYearMonth() {
        try {
            List<Map<String, Object>> yearMonthList = commonService.yearMonthList();
            List<Map<String, Object>> finalYearMonthList = new ArrayList<>();
            Integer[] status = {0,3};
            for(Map<String, Object> yarMonth : yearMonthList){
                QueryWrapper<TkIndexValueAudit> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(TkIndexValueAudit::getIndexGroup,"A01A01A01");
                queryWrapper.lambda().eq(TkIndexValueAudit::getDataType,"kpi");
                queryWrapper.lambda().in(TkIndexValueAudit::getAuditStatus, Arrays.asList(status));
                queryWrapper.lambda().eq(TkIndexValueAudit::getIndexValueY,yarMonth.get("value").toString().substring(0,4));
                queryWrapper.lambda().eq(TkIndexValueAudit::getIndexValueM,yarMonth.get("value").toString().substring(4));
                int count = tkIndexValueAuditService.count(queryWrapper);
                if(count > 0){
                    finalYearMonthList.add(yarMonth);
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("indexYearMonth",finalYearMonthList);
            List<Map<String, Object>> indexDepartGroupList = new ArrayList();
            Map<String, Object> departGroup = new HashMap<>();
            departGroup.put("value","");
            departGroup.put("text","全国");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G01");
            departGroup.put("text","燕园");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G02");
            departGroup.put("text","申园");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G03");
            departGroup.put("text","粤园");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G04");
            departGroup.put("text","蜀园");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G05");
            departGroup.put("text","吴园");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G06");
            departGroup.put("text","楚园");
            indexDepartGroupList.add(departGroup);
            departGroup = new HashMap<>();
            departGroup.put("value","A01A01A01G07");
            departGroup.put("text","大清谷");
            indexDepartGroupList.add(departGroup);
            map.put("indexDepartGroupList",indexDepartGroupList);
            List<Map<String, Object>> dimensionalityList = new ArrayList();
            Map<String, Object> dimensionality = new HashMap<>();
            dimensionality.put("value","growAndValue");
            dimensionality.put("text","增长与价值维度");
            dimensionalityList.add(dimensionality);
            dimensionality = new HashMap<>();
            dimensionality.put("value","fuseAndBiz");
            dimensionality.put("text","融合与业态");
            dimensionalityList.add(dimensionality);
            map.put("dimensionalityList",dimensionalityList);
            return Result.ok(map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }
}
